﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHMS.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Collections;
using WMS.HelperClasses;

namespace WMS
{
    public partial class SearchShelfForm : Form
    {

        public string SelectedShelfCode { get; set; }
        EntityField2 NumberField { get; set; }
        EntityField2 StoreField { get; set; }
       
        EntityRelation storeRelation { get; set; }
        RelationPredicateBucket Filter { get; set; }


        public SearchShelfForm(EntityField2 numberField, EntityField2 storeField,  RelationPredicateBucket filter, Func<DataTable> storesRetrievalFunc)
		{
			InitializeComponent();
            NumberField = numberField;
            StoreField = storeField;

            //storeRelation = (EntityRelation)st;
            Filter = filter;

          
			var func = (storesRetrievalFunc == null)? () => Business.GetStoreDetails(null) : storesRetrievalFunc;
           
            FormsHelper.LoadComboBox(DDLStores, StoreFields.Id, StoreFields.Name,func );
		}

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var fields = new ResultsetFields(2);
            fields.DefineField(ShelfFields.Code, 0, "ShelfCode");
            fields.DefineField(StoreFields.Name, 1, "Store");
           

            var filter = new RelationPredicateBucket();
            if (Filter != null)
            {
                //Filter = new RelationPredicateBucket();				
                filter.Relations.AddRange((ICollection)Filter.Relations);
                filter.PredicateExpression.Add(Filter.PredicateExpression);

            }
           

            if (SearchNumberCB.Checked && !string.IsNullOrEmpty(txtShelfCode.Text))
            {
                var numberTerm = txtShelfCode.Text + "%";
                filter.PredicateExpression.Add(NumberField % numberTerm);
            }

            if (SearchByStoreCB.Checked)
            {
                filter.PredicateExpression.Add(StoreField == GetSelectedStoreId());
            }

            var dt = DALHelper.GetDynamicList(fields, filter, new SortExpression(NumberField | SortOperator.Ascending));
            dataGridView1.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = true;
            }
        }

        private int GetSelectedStoreId()
        {
            if (DDLStores.SelectedItem == null)
                return 0;

            return (int)((System.Data.DataRowView)(DDLStores.SelectedItem)).Row.ItemArray[0];
        }

        private void SearchByStoreCB_CheckedChanged(object sender, EventArgs e)
        {
            if (SearchByStoreCB.Checked)
            {
                DDLStores.Enabled = true;
                SearchNumberCB.Checked = false;
                txtShelfCode.Enabled = false;
            }
        }

        private void SearchNumberCB_CheckedChanged(object sender, EventArgs e)
        {
            if (SearchNumberCB.Checked)
            {
                DDLStores.Enabled = false;
                SearchByStoreCB.Checked = false;
                txtShelfCode.Enabled = true;
            }
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                SelectedShelfCode = (string)dataGridView1.SelectedRows[0].Cells[ShelfCode.Index].Value;
            }

            this.DialogResult = DialogResult.OK;

            this.Close();
        }
    }
}
